<?php

declare(strict_types=1);
/**
 * 
 */
namespace WanZ\App\User\Service;

use Hyperf\Database\Model\Builder;
use WanZ\App\User\Contract\WzUserAttributeContract;
use WanZ\App\User\Model\WzUserAttribute;
use MoChat\Framework\Service\AbstractService;

class WzUserAttributeService extends AbstractService implements WzUserAttributeContract
{
    /**
     * @var WzUserAttribute
     */
    protected $model;

    /**
     * @var DEFAULT_FIELDS
     */
    private const DEFAULT_FIELDS = [
        'user_id',
        'nickname',
        'head_img_url',
        'wx_openid',
        'wx_mini_openid',
        'subscribe',
        'wx_unionid',
        'ali_openid',
        'wx_balance',
        'ali_balance',
        'is_open_charge_safeguard',
        'is_open_charge_safeguard_notice',
        'is_show_claims',
        'is_charge_notice'
    ];   

    /**
     * 查询单条 - 根据用户ID.
     * @param int $userId 用户ID
     * @param array|string[] $columns 查询字段
     * @return null|\App\Model\WzUserAttribute|\Hyperf\Database\Model\Builder|object
     */
    public function getUserAttrByUserId(int $userId, array $columns = [])
    {
        $columns = $columns ?: self::DEFAULT_FIELDS;
        return $this->model::query()->where('user_id', $userId)->first($columns);
    }

    /**
     * 修改单条 - 根据ID.
     * @param int $id ID
     * @param array $data 修改数据
     * @return int 修改条数
     */
    public function updateUserAttrById(int $id, array $data): int
    {
        return $this->model->updateOneById($id, $data);
    }

    /**
     * 修改单条 - 根据用户ID.
     * @param int $userId 用户ID
     * @param array $data 修改数据
     * @return int 修改条数
     */
    public function updateUserAttrByUserId(int $userId, array $data): int
    {
        return $this->model::query()->where('user_id', $userId)->update($data);
    }

    /**
     * 删除 - 单条
     * @param int $userId 用户ID
     * @return int 删除条数
     */
    public function deleteUserAttrByUserId(int $userId): int
    {
        return $this->model::query()->where('user_id', $userId)->delete();
    }

    /**
     * 添加单条
     * @param array $data 添加的数据
     * @return int 自增ID
     */
    public function createUserAttr(array $data): int
    {
        return $this->model->createOne($data);
    }

    /**
     * 查询单条 - 根据微信openID.
     * @param string $wxMiniOpenId
     * @param array $columns 查询字段
     * @return array 响应数组
     */
    public function getUserAttrByWxMiniOpenId(string $wxMiniOpenId, array $columns = []): array
    {
        $columns = $columns ?: self::DEFAULT_FIELDS;
        $res = $this->model::query()
            ->where('wx_mini_openid', $wxMiniOpenId)
            ->first($columns);

        $res || $res = collect([]);
        return $res->toArray();            
    }  
    
    /**
     * 查询单条 - 根据微信unionID.
     * @param string $wxUnionId
     * @param array $columns 查询字段
     * @return array 响应数组
     */
    public function getUserAttrByWxUnionId(string $wxUnionId, array $columns = []): array
    {
        $columns = $columns ?: self::DEFAULT_FIELDS;
        $res = $this->model::query()
            ->where('wx_unionid', $wxUnionId)
            ->first($columns);

        $res || $res = collect([]);
        return $res->toArray();  
    }    

    /**
     * 查询单条 - 根据阿里openID.
     * @param string $aliMiniOpenId
     * @param array $columns 查询字段
     * @return array 响应数组
     */
    public function getUserAttrByAliMiniOpenId(string $aliMiniOpenId, array $columns = []): array
    {
        $columns = $columns ?: self::DEFAULT_FIELDS;
        $res = $this->model::query()
            ->where('ali_openid', $aliMiniOpenId)
            ->first($columns);

        $res || $res = collect([]);
        return $res->toArray();            
    } 

}
